package ciyuan.Gui.Component;

import ciyuan.Constants.PlayerDataType;
import ciyuan.DataManager.PlayerDataManager;
import ciyuan.MainClass;
import ciyuan.Network.AddAttrbutePacket;
import ciyuan.Network.NetWorkRegister;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;

public class AddPointWidget extends Button {

    private static final StringTextComponent stringTextComponent = new StringTextComponent("");
    private static final ResourceLocation BACKGROUND_RESOURCE = new ResourceLocation(MainClass.MODID, "textures/gui/player_info/add_point_widget.png");

    private final ITextComponent labelText1;
    private final ITextComponent labelText2;
    private final PlayerDataType playerDataType;

    public AddPointWidget(int left, int top, PlayerDataType playerDataType) {
        super(left, top, 15, 41, stringTextComponent, (Button) -> NetWorkRegister.INSTANCE.sendToServer(new AddAttrbutePacket(playerDataType.getName())));
        this.playerDataType = playerDataType;
        this.labelText1 = new TranslationTextComponent("ciyuan.gui.playerInfo.label." + playerDataType.getName() + 1);
        this.labelText2 = new TranslationTextComponent("ciyuan.gui.playerInfo.label." + playerDataType.getName() + 2);
    }

    @Override
    public void renderButton(MatrixStack matrixStack, int mouseX, int mouseY, float particleTick) {
        this.renderBg(matrixStack, Minecraft.getInstance(), mouseX, mouseY);

        drawString(matrixStack, Minecraft.getInstance().font, String.valueOf(PlayerDataManager.getInt(null, playerDataType)), this.x + 7, this.y + 23, 16777215);
        drawString(matrixStack, Minecraft.getInstance().font, labelText1, this.x + 4, this.y + 2, 16777215);
        drawString(matrixStack, Minecraft.getInstance().font, labelText2, this.x + 4, this.y + 12, 16777215);
    }

    @Override
    protected void renderBg(MatrixStack matrixStack, Minecraft minecraft, int mouseX, int mouseY) {
        minecraft.getTextureManager().bind(BACKGROUND_RESOURCE);
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, this.alpha);
        blit(matrixStack, this.x, this.y, 0, 0, width, height, width, height);
    }
}
